/*	TGConfigureSheetController.m
 *	Part of ThinkGeek LED Clock
 *	http://cs.beloit.edu/~morgant/thinkgeek/
 *
 * Copyright (c)2003-2005 Tim Morgan. All rights reserved.
 * Please visit http://cs.beloit.edu/~morgant/other/license.html
 * for more information about your rights and responsibilities
 * regarding this source code.
 */

#import "TGConfigureSheetController.h"

TGConfigureSheetController *configureSheetControllerSingleton;

@implementation TGConfigureSheetController

#pragma mark <<initialization>>

+ (void) configureTransformers {
	// Set up the transformers before loading the nib
	TMModelKeyFromViewNameTransformer *backgroundStyleTransformer = [[TMModelKeyFromViewNameTransformer alloc] init];
	[backgroundStyleTransformer associateViewName:@"Default" withModelKey:[TGStringConstants backgroundStyleDefault]];
	[backgroundStyleTransformer associateViewName:@"Custom color:" withModelKey:[TGStringConstants backgroundStyleCustomColor]];
	[backgroundStyleTransformer associateViewName:@"Desktop" withModelKey:[TGStringConstants backgroundStyleDesktop]];
	[backgroundStyleTransformer associateViewName:@"Custom image:" withModelKey:[TGStringConstants backgroundStyleCustomImage]];
	
	TM_FileNameFromPathTransformer *imageFileNameTransformer = [[TM_FileNameFromPathTransformer alloc] init];
	
	[NSValueTransformer setValueTransformer:[imageFileNameTransformer autorelease] forName:@"TGBackgroundImageFilePathTransformer"];
	[NSValueTransformer setValueTransformer:[backgroundStyleTransformer autorelease] forName:@"TGBackgroundStyleTransformer"];
}

- (void) awakeFromNib {
	configureSheetControllerSingleton = self;
}

+ (TGConfigureSheetController *) controller {
	if (configureSheetControllerSingleton == nil)
		[[NSException exceptionWithName:@"TGUnexpectedInvocationException"
								 reason:@"Tried to access the TGConfigureSheetController singleton before it had been created."
							   userInfo:nil] raise];
	return configureSheetControllerSingleton;
}

#pragma mark -
#pragma mark <<user interface>>

- (NSWindow *) configureSheet {
	return configureSheet;
}

#pragma mark -
#pragma mark <<user input>>

- (IBAction) doneButtonPressed:(id)sender {
	[NSApp endSheet:configureSheet]; // does nothing if in application mode
	if ([configureSheet isVisible]) [configureSheet close]; // does nothing if in screensaver mode
	[[TGDefaultsLoader loader] writeDefaults];
}

- (IBAction) chooseImageButtonPressed:(id)sender {
	NSOpenPanel	*openPanel = [NSOpenPanel openPanel];
	NSArray		*chosenFiles;
	
	[openPanel setAllowsMultipleSelection:NO];
	
	if ([openPanel runModalForTypes:[NSImage imageFileTypes]] == NSOKButton) {
		chosenFiles = [openPanel filenames];
		NSString *customImagePath = [[NSString alloc] initWithString:[chosenFiles objectAtIndex:0]];
		[defaultsModel setBackgroundImageFilePath:[customImagePath autorelease]];
    }
}

- (IBAction) bcdClockSiteButtonPressed:(id)sender {
}

- (IBAction) tixClockSiteButtonPressed:(id)sender {
}

- (IBAction) checkNowButtonPressed:(id)sender {
}

@end
